package com.common.widget.form;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.R;
import com.common.utility.Logger;


public class FormImageIntentView extends RelativeLayout implements View.OnClickListener {

    private String mTagString;
    private String mTextString;
    private String mHintString;
    private Boolean mShowDivider;
    private boolean mFormHasArrow;
    private String mIntentString;

    private boolean mHasData;
    private String mValue;

    private boolean mClickable;//是否能被点击
    private Bundle mIntentValue = new Bundle();

    private LinearLayout mFormContentView;

    private TextView mTagTextView;
    private TextView mTextView;
    private View mDividerView;
    private ImageView mArrowView;

    private ImageView mLeftIcon;
    private int mArrowViewResource;

    private int leftIconResource;


    public FormImageIntentView(Context context) {
        super(context);
        init(null, 0);
    }

    public FormImageIntentView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public FormImageIntentView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {

        // Load attributes
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.FormView, defStyle, 0);

        mTagString = a.getString(
                R.styleable.FormView_form_tag);
        mTextString = a.getString(
                R.styleable.FormView_form_text);

        mHintString = a.getString(
                R.styleable.FormView_form_hint);
        mShowDivider = a.getBoolean(
                R.styleable.FormView_form_divider, true);

        mFormHasArrow = a.getBoolean(R.styleable.FormView_form_has_arrow,true);

        mHasData = a.getBoolean(R.styleable.FormView_form_hasdata,false);
        mIntentString = a.getString(R.styleable.FormView_form_intent);
        mArrowViewResource = a.getResourceId(R.styleable.FormView_form_arrow_icon,R.drawable.arrow_right);
        mClickable = a.getBoolean(R.styleable.FormView_form_clickable,true);

        leftIconResource = a.getResourceId(R.styleable.FormView_form_left_icon,R.drawable.my_item_1);
        a.recycle();

        LayoutInflater.from(getContext()).inflate(R.layout.view_form_img_intent_view, this, true);
        mFormContentView = (LinearLayout) findViewById(R.id.form_content);
        mTagTextView = (TextView) findViewById(R.id.form_tag);

        mTextView = (TextView) findViewById(R.id.form_text);
        mTextView.setMaxLines(2);
       // mTextView.setSingleLine(true);
        mTextView.setEllipsize(TextUtils.TruncateAt.END);

        mDividerView = findViewById(R.id.form_divider);
        mArrowView = (ImageView)findViewById(R.id.iv_arrow);
        mLeftIcon = (ImageView)findViewById(R.id.iv_item_icon);

        if (mTagString != null) {
            mTagTextView.setText(mTagString);
        }

        mTagTextView.setTextColor(getResources().getColor(R.color.form_item_tag_text_color));

        if (mTextString != null) {
            mTextView.setText(mTextString);
            mArrowView.setImageResource(mArrowViewResource);
        }

        if(!TextUtils.isEmpty(mHintString)){
            mTextView.setHint(mHintString);
        }

        mLeftIcon.setImageResource(leftIconResource);
        mArrowView.setVisibility(mFormHasArrow ?  View.VISIBLE : View.GONE);

        mDividerView.setVisibility(mShowDivider ? View.VISIBLE : View.GONE);

        mFormContentView.setOnClickListener(this);
        mTextView.setOnClickListener(this);
    }

    public void setTag(String tag) {
        mTagTextView.setText(tag);
    }

    public void setText(String text) {
        if (!TextUtils.isEmpty(text)) {
            mTextView.setText(text);
        }
    }

    public void setArrowResource(int resId){
        if(mArrowView != null){
            mArrowView.setImageResource(resId);
        }
    }

    public void setTextColor(int color){
        mTextView.setTextColor(color);
    }

    public String getText() {
        return mTextView.getText().toString();
    }

    public String getValue() {
        return mValue;
    }
    public void setValue(String mValue) {
        this.mValue = mValue;
    }

    public void setIntentData(Bundle bundle){
        this.mIntentValue = bundle;
    }

    public void setIntentAction(String action){
        this.mIntentString = action;
    }

    @Override
    public void onClick(View view) {
        if(mClickable){
            if(TextUtils.isEmpty(mIntentString)){
                Logger.e("mark", "action is empty!!!");
                return;
            }
            Intent intent = new Intent();
            intent.setClassName(getContext().getPackageName(), mIntentString);
            if(mHasData){
                intent.putExtras(mIntentValue);
            }
            getContext().startActivity(intent);
        }

    }

    public void setHasData(boolean hasData){
        mHasData =  hasData;
    }

    public void setClickable(boolean clickable){
        this.mClickable = clickable;
        mArrowView.setVisibility(clickable ?  View.VISIBLE : View.GONE);
    }
}
